package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.api.common.ResUlt;
import top.api.domain.dto.CourseAddDto;
import top.api.domain.dto.CourseEditDto;
import top.api.domain.dto.PageDto;
import top.api.domain.entity.Class;
import top.api.domain.entity.Course;
import top.api.domain.entity.Student;
import top.api.domain.vo.PageVo;
import top.api.service.CourseService;
import top.api.mapper.CourseMapper;
import org.springframework.stereotype.Service;
import top.api.utils.AssertUtil;
import top.api.utils.BeanCopyUtils;

import java.util.List;

/**
* @author 27465
* @description 针对表【course】的数据库操作Service实现
* @createDate 2023-12-17 11:27:41
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Override
    public ResUlt<?> pageList(PageDto pageDto, Integer id, String coursename) {
        Page<Course> pageInfo = new Page<>(pageDto.getPageNum(),pageDto.getPageSize());

        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotNull(id),Course::getId,id);
        queryWrapper.like(StringUtils.isNotBlank(coursename),Course::getCoursename,coursename);

        baseMapper.selectPage(pageInfo,queryWrapper);

        PageVo<Course> pageVo = new PageVo<>(pageInfo.getRecords(),pageInfo.getTotal());

        return ResUlt.success(pageVo);
    }

    @Override
    public ResUlt<?> getCourseInfo(Integer id) {
        return ResUlt.success(baseMapper.selectById(id));
    }

    @Override
    public ResUlt<?> addCourse(CourseAddDto courseAddDto) {
        Course course = BeanCopyUtils.copyBean(courseAddDto, Course.class);

        // 添加no
        course.setCourseno(IdWorker.getIdStr());

        // 判断课程名是否存在
        Long count = baseMapper.selectCount(Wrappers.<Course>lambdaQuery()
                .eq(Course::getCoursename, courseAddDto.getCoursename()));
        AssertUtil.isTrue(count > 0,"课程名已存在");

        int flag = baseMapper.insert(course);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> editCourse(CourseEditDto courseEditDto) {
        Course course = BeanCopyUtils.copyBean(courseEditDto, Course.class);

        // 判断用户名是否存在 不包括自己
        Long count = baseMapper.selectCount(Wrappers.<Course>lambdaQuery()
                .eq(Course::getCoursename, course.getCoursename())
                .ne(Course::getId,course.getId()));
        AssertUtil.isTrue(count > 0,"班级名已存在");

        int flag = baseMapper.updateById(course);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> delCourse(List<Integer> ids) {
        int count = baseMapper.deleteBatchIds(ids);

        return ResUlt.success("删除成功: ["+count+"]");
    }
}




